﻿using System;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace DoubanFM.ViewModel
{
    /// <summary>
    /// Base class for all the view model
    /// </summary>
    [Windows.Foundation.Metadata.WebHostHidden]
    public abstract class ViewModelCommon : Common.BindableBase
    {
        private static readonly Uri BaseUri = new Uri("ms-appx:///");

        protected ViewModelCommon(string uniqueId, string title, string smallImagePath, String largeImagePath, string description)
        {
            _uniqueId = uniqueId;
            _title = title;
            _description = description;
            _smallImagePath = smallImagePath;
            _largeImagePath = largeImagePath;
        }

        private string _uniqueId = string.Empty;
        public string UniqueId
        {
            get { return _uniqueId; }
            set { SetProperty(ref _uniqueId, value); }
        }

        private string _title = string.Empty;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _description = string.Empty;
        public string Description
        {
            get { return _description; }
            set { SetProperty(ref _description, value); }
        }

        private ImageSource _smallImage;
        private string _smallImagePath;
        public ImageSource SmallImage
        {
            get
            {
                if (_smallImage == null && _smallImagePath != null)
                {
                    _smallImage = new BitmapImage(new Uri(BaseUri, _smallImagePath));
                }
                return _smallImage;
            }
            set
            {
                _smallImagePath = null;
                SetProperty(ref _smallImage, value);
            }
        }

        public string SmallImagePath
        {
            get { return _smallImagePath; }
            set
            {
                _smallImage = null;
                SetProperty(ref _smallImagePath, value);
                // ReSharper disable ExplicitCallerInfoArgument
                OnPropertyChanged("SmallImage");
                // ReSharper restore ExplicitCallerInfoArgument 
            }
        }

        private ImageSource _largeImage;
        private string _largeImagePath;
        public ImageSource LargeImage
        {
            get
            {
                if (_largeImage == null && _largeImagePath != null)
                {
                    _largeImage = new BitmapImage(new Uri(BaseUri, _largeImagePath));
                }
                return _largeImage;
            }
            set
            {
                _largeImagePath = null;
                SetProperty(ref _largeImage, value);
            }
        }

        public string LargeImagePath
        {
            get { return _largeImagePath; }
            set
            {
                _largeImage = null;
                SetProperty(ref _largeImagePath, value);
                // ReSharper disable ExplicitCallerInfoArgument
                OnPropertyChanged("SmallImage");
                // ReSharper restore ExplicitCallerInfoArgument 
            }
        }

        public override string ToString()
        {
            return Title;
        }
    }
}
